#include "server_global_config.h"

LibSystemConfig        system_config = {
    .server_port = 8989, // 默认服务器端口
    .max_connections = 5 // 默认最大连接数
};

BookManageConfig    book_config   = {
    .stu_max_borrow_days = 30,      // 学生最大借阅天数
    .teacher_max_borrow_days = 60,  // 教师最大借阅天数
    .stu_max_borrow_books = 2,      // 学生最大借阅数量
    .teacher_max_borrow_books = 4,  // 教师最大借阅数量
    .stu_renew_days = 15,           // 学生续借天数
    .teacher_renew_days = 30,       // 教师续借天数
    .stu_renew_max_count = 2,       // 学生最大续借次数
    .teacher_renew_max_count = 5,   // 教师最大续借次数
    .next_book_id = 1,               // 下一个图书ID初始值
    .next_user_id = 1,              // 下一个用户ID初始值
};

uint8 server_load_config() {
    server_log(LOG_SYSTEM, "Loading server configuration...\n");
    // 系统配置文件加载
    FILE * system_config_file = fopen(SYSTEM_CONFIG_FILE_PATH, "rb");
    if (system_config_file == NULL) {
        server_log(LOG_SYSTEM, "Failed to open system config file: %s\n", SYSTEM_CONFIG_FILE_PATH);
        return 1;
    }
    if (fread(&system_config, sizeof(LibSystemConfig), 1, system_config_file) != 1) {
        server_log(LOG_SYSTEM, "Failed to read system config file: %s\n", SYSTEM_CONFIG_FILE_PATH);
        fclose(system_config_file);
        return 1;
    }
    fclose(system_config_file);

    // 图书管理配置文件加载
    FILE * book_config_file = fopen(BOOK_MANAGE_CONFIG_FILE_PATH, "rb");
    if (book_config_file == NULL) {
        server_log(LOG_SYSTEM, "Failed to open book manage config file: %s\n", BOOK_MANAGE_CONFIG_FILE_PATH);
        return 1;
    }
    if (fread(&book_config, sizeof(BookManageConfig), 1, book_config_file) != 1) {
        server_log(LOG_SYSTEM, "Failed to read book manage config file: %s\n", BOOK_MANAGE_CONFIG_FILE_PATH);
        fclose(book_config_file);
        return 1;
    }
    fclose(book_config_file);
    server_log(LOG_SYSTEM, "Server configuration loaded successfully.\n");
    return 0;
}
uint8 server_save_config() {
    server_log(LOG_SYSTEM, "Saving server configuration...\n");
    // 系统配置文件加载
    FILE * system_config_file = fopen(SYSTEM_CONFIG_FILE_PATH, "wb");
    if (system_config_file == NULL) {
        server_log(LOG_SYSTEM, "Failed to open system config file: %s\n", SYSTEM_CONFIG_FILE_PATH);
        return 1;
    }
    if (fwrite(&system_config, sizeof(LibSystemConfig), 1, system_config_file) != 1) {
        server_log(LOG_SYSTEM, "Failed to write system config file: %s\n", SYSTEM_CONFIG_FILE_PATH);
        fclose(system_config_file);
        return 1;
    }
    fclose(system_config_file);

    // 图书管理配置文件加载
    FILE * book_config_file = fopen(BOOK_MANAGE_CONFIG_FILE_PATH, "wb");
    if (book_config_file == NULL) {
        server_log(LOG_SYSTEM, "Failed to open book manage config file: %s\n", BOOK_MANAGE_CONFIG_FILE_PATH);
        return 1;
    }
    if (fwrite(&book_config, sizeof(BookManageConfig), 1, book_config_file) != 1) {
        server_log(LOG_SYSTEM, "Failed to write book manage config file: %s\n", BOOK_MANAGE_CONFIG_FILE_PATH);
        fclose(book_config_file);
        return 1;
    }
    fclose(book_config_file);

    server_log(LOG_SYSTEM, "Server configuration saved successfully.\n");
    return 0;
}
